package lobysocket
{
	import flash.events.Event;
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import lobystate.StateManager;

	public class SocketLoginGame extends SocketRequestState
	{
		private static var instance:SocketLoginGame = null;

		public var userId:String="";
		public function SocketLoginGame()
		{
			super();
		}

		public static function get Instance():SocketLoginGame
		{
			if(instance == null)
				instance = new SocketLoginGame();
			return instance;
		}

		override public function send(obj:SocketManager):void
		{
			if (FlexGlobals.topLevelApplication.txt_username.text.length<1) {
				Alert.show('input username');
				return;
			}
			if (FlexGlobals.topLevelApplication.txt_password.text.length<1) {
				Alert.show('input password');
				return;
			}		
			// 互斥量有效
			LobyNetManager.Instance.showNetProcess("……");
			LobyManager.Instance.windowMutex = true;
			
			LobyNetManager.Instance.socketSend("1~1~LOGIN_SIGN_IN~"+FlexGlobals.topLevelApplication.txt_username.text+"~"+FlexGlobals.topLevelApplication.txt_password.text);
		}
		override public function receive(obj:Object):Boolean
		{
			if(super.receive(obj))
			{
			var results:Array=obj.split("~",4);
			switch(results[2].toString())
			{
				case "LOGIN_ERROR_USERNAME_OR_PASSWORD":						
					Alert.show("用户名或密码错误\n");  
					break;
				case "LOGIN_USER_ALREADY_ONLINE":
					Alert.show(" 玩家已经登陆，请换其他账号\n");  
					break;
				case "LOGIN_SUCCESS":
					trace("success");	
					this.userId=results[3];
					FlexGlobals.topLevelApplication.currentState = "GameLobby";
					break;
			}
			}
			return true;
		}
		override public function fault(event:Event):void
		{
			// 关闭互斥量
			LobyManager.Instance.windowMutex = false;
			LobyErrorState.Instance.showErrMsg("登陆失败,意外失败");
		}
	}
}